<template>
  <div>
    <n-modal
      v-if="props.showModal"
      v-model:show="props.showModal"
      :mask-closable="true"
      @update:show="$emit('CheckShowModal', false)"
    >
      <n-card
        style="width: 600px"
        title="编辑角色"
        :bordered="false"
        size="huge"
        role="dialog"
        aria-modal="true"
      >
        <template #header-extra>
          <n-button type="error" @click="$emit('CheckShowModal', false)">
            X
          </n-button>
        </template>
        <n-form ref="formRef" :model="model" :rules="rules">
          <n-form-item path="uName" label="用户名">
            <n-input v-model:value="model.rName" placeholder="请输入用户名" />
          </n-form-item>
          <n-checkbox-group v-model:value="cities" @click="change">
            <n-space item-style="display: flex;">
              <n-checkbox
                v-for="item in menu"
                :key="item.nId"
                :value="item.nId"
                :label="item.nName"
              />
            </n-space>
          </n-checkbox-group>
          <n-row :gutter="[0, 24]">
            <n-col :span="24">
              <div style="display: flex; justify-content: center">
                <n-button round type="primary" @click="UpdateUserFile">
                  修改
                </n-button>
              </div>
            </n-col>
          </n-row>
        </n-form>
      </n-card>
    </n-modal>
    <n-skeleton v-else text :repeat="2" />
    <n-skeleton text style="width: 60%" />
  </div>
</template>

<script lang="ts" setup>
import { defineProps, ref, reactive, onMounted } from "vue";
import { GetRolesInfo, EditRolesInfo } from "@/api/rolse";
import { getNav } from "@/api/menu";
const formRef = ref();
const emit = defineEmits(["CheckShowModal", "reloadpage"]);
const props = defineProps({
  showModal: {
    type: Boolean,
    default: false,
  },
  userId: {
    type: Number,
    default: "",
  },
});
const menu: any = ref([]);
const cities: any = ref([]);
const model = reactive({
    rId:0,
    rName: "",
  rPower:''
});
const params = reactive({
  rId: 1,
});
onMounted(() => {
    params.rId = props.userId;
    model.rId = props.userId;
  if (props.userId) {
    GetRolesInfo(params).then((res: any) => {
      model.rName = res.data.rName;
      cities.value = res.data.rPower.split(",");
    });
  }
});

const rules = {
  rName: [
    {
      required: true,
      message: "请输入姓名",
      trigger: ["input", "blur"],
    },
  ]
};
getNav({}).then((res) => {
  res.data.data.forEach((item: any) => {
    menu.value.push({ nId: item.nId, nName: item.nName });
  });
});
const change = () => {
  model.rPower = cities.value.join(",");
};
const UpdateUserFile = (e: Event) => {
  e.preventDefault();
  // console.log(model)
  formRef.value.validate((err: any) => {
    if (err) {
      console.log(err);
    } else {
      EditRolesInfo(model).then((res: any) => {
        emit("CheckShowModal", false);
        emit("reloadpage");
      });
    }
  });
};
</script>

<style scoped lang="scss"></style>
